<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="QT是一个跨平台的C++应用程序开发框架。支持Linux、Windows、Mac、iOS、Android等几乎所有的平台。详见维基百科[https://zh.wikipedia.org/wiki/Qt]
F1:帮助文档，不熟悉QT的情况下，F1是最有用的快捷键了。
QT创建应用程序时自带三种视图：
">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="QT基础1">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="QT是一个跨平台的C++应用程序开发框架。支持Linux、Windows、Mac、iOS、Android等几乎所有的平台。详见维基百科[https://zh.wikipedia.org/wiki/Qt]
F1:帮助文档，不熟悉QT的情况下，F1是最有用的快捷键了。
QT创建应用程序时自带三种视图：
">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>QT基础1 - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>QT基础1</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2018-12-12
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        

<a href="/blog/categories/QT/">QT</a>

                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <p>QT是一个跨平台的C++应用程序开发框架。支持Linux、Windows、Mac、iOS、Android等几乎所有的平台。<br>详见维基百科[<a href="https://zh.wikipedia.org/wiki/Qt]" target="_blank" rel="noopener">https://zh.wikipedia.org/wiki/Qt]</a></p>
<p>F1:帮助文档，不熟悉QT的情况下，F1是最有用的快捷键了。</p>
<p>QT创建应用程序时自带三种视图：</p>
<p>1、QMainWindow: 用于PC端，带菜单栏</p>
<p>2、QDialog: 对话框 </p>
<p>3、QWidget: QT视图基类，相当于iOS的UIView<br>一般可以选用QMainWindow或QWidget。</p>
<p>QT文件结构<br><img src="http://cyrus_huang.gitee.io/blog/2018/12/12/QT基础1/qt_files.png" alt=""></p>
<p>首先，看一下main文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#include  “mywidget.h&quot;</span><br><span class="line">//应用程序类</span><br><span class="line">#include &lt;QApplication&gt;</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">&#123;</span><br><span class="line">	//有且只有一个应用程序类的对象，配置运行环境</span><br><span class="line">	QApplication a(argc, argv);</span><br><span class="line">	//创建窗口</span><br><span class="line">	MyWidget w;</span><br><span class="line">	//显示窗口</span><br><span class="line">	w.show();</span><br><span class="line"></span><br><span class="line">	//运行程序，死循环，等待事件发生，相当于iOS的 主线程runloop</span><br><span class="line">	//思路与iOS的main函数相当，只是iOS是在AppDelegate中创建界面</span><br><span class="line">	return a.exec();</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure></p>
<p>.pro配置文件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#模块 头文件 f1 查找文档可以找到所有QT类需要添加的模块</span><br><span class="line">QT += core gui</span><br><span class="line"></span><br><span class="line">#高于4的版本，添加 QT += widgets, 为了兼容QT4</span><br><span class="line">greaterThan(QT_MAJOR_VERSION, 4): QT += widgets</span><br><span class="line"></span><br><span class="line">#应用程序的名字</span><br><span class="line">TARGET = QtTest</span><br><span class="line">#指定makefile的类型， app/lib库</span><br><span class="line">TEMPLATE = app</span><br><span class="line"></span><br><span class="line">#使用C++11需要添加</span><br><span class="line">CONFIG += c++11</span><br><span class="line"></span><br><span class="line">#源文件，.cpp文件，创建类后自动添加</span><br><span class="line">SOURCES += \</span><br><span class="line">main.cpp \</span><br><span class="line">mywidget.cpp</span><br><span class="line"></span><br><span class="line">#头文件 .h文件，创建类后自动添加</span><br><span class="line">HEADERS += \</span><br><span class="line">mywidget.h</span><br></pre></td></tr></table></figure></p>
<p><font color="ff0000">这里，QT += 模块，可以在使用相关类时，在类名处按F1,进入帮助文档界面查找。</font><br><img src="http://cyrus_huang.gitee.io/blog/2018/12/12/QT基础1/help_add_frame.png" alt=""></p>
<p>最后，看一下代码创建控件<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">//mywidget.h</span><br><span class="line"></span><br><span class="line">#ifndef MYWIDGET_H</span><br><span class="line">#define MYWIDGET_H</span><br><span class="line"></span><br><span class="line">#include &lt;QWidget&gt;</span><br><span class="line">#include &lt;QPushButton&gt;</span><br><span class="line"></span><br><span class="line">class MyWidget : public QWidget</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line">public:</span><br><span class="line">    explicit MyWidget(QWidget *parent = nullptr);</span><br><span class="line"></span><br><span class="line">signals:</span><br><span class="line"></span><br><span class="line">public slots:</span><br><span class="line">    void mySlot();</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">	//非指针对象需要添加为成员变量</span><br><span class="line">    QPushButton btn;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">#endif // MYWIDGET_H</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//mywidget.cpp</span><br><span class="line">#include &quot;mywidget.h&quot;</span><br><span class="line">#include &lt;QLabel&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">MyWidget::MyWidget(QWidget *parent) : QWidget(parent)</span><br><span class="line">&#123;</span><br><span class="line">	//指针变量，用new创建</span><br><span class="line">    QLabel *lb = new QLabel(this);</span><br><span class="line">    //设置内容</span><br><span class="line">    lb-&gt;setText(QString(&quot;hello world&quot;));</span><br><span class="line"></span><br><span class="line">	//成员变量</span><br><span class="line">    btn.setParent(this);</span><br><span class="line">    btn.setText(&quot;hehe&quot;);</span><br><span class="line">    btn.move(50, 50);</span><br><span class="line"></span><br><span class="line">	//错误的创建方法,为局部变量，父视图没有强引用控件，不显示</span><br><span class="line">    QLabel lb(this);</span><br><span class="line">    //设置内容</span><br><span class="line">    lb.setText(QString(&quot;hello world&quot;));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>